<?php 
namespace Admin\Controller;
/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class AttrvalController extends AdminController{
public function index(){
		$kid=I('get.kid');
		$gid=I('get.gid');
		$res=M('attrval')->where(array('kid'=>$kid))->select();
		$attr = M('attr')->where(array('id'=>$kid))->field('name')->find();
        $goods = M('goods')->where(array('id'=>$gid))->field('name')->find();
		$this->assign('res',$res);
		$this->assign('kid',$kid);
		$this->assign('gid',$gid);
		$this->assign('attr', $attr);
        $this->assign('goods', $goods);
		// var_dump($res);
		$this->display();
	}
	public function add(){
		if (IS_POST) {
			$attrval = D('Attrval');
			$res=$attrval->create();
			if ($res) {
                if ($attrval->add($res)) {
                    $this->success('添加成功',U('index'));
                }else{
                    $this->error('添加失败',U('add'));
                }
            }else{
                $this->error($attrval->getError());
            }
		}else{
			$kid=I('get.kid');		
		    $gid=I('get.gid');
			$res=M('attr')->where(array('id'=>$kid))->find();
			// var_dump($res);
			$this->assign('res',$res);
			$this->assign('kid',$kid);
		    $this->assign('gid',$gid);
			$this->display();
		}
		
	}
	public function edit(){
        if(IS_POST){
            $attrval = D('attrval');
            $data = $attrval->create();
            if($data){
                if($attrval->save()!== false){
                    $this->success('修改成功');
                } else {
                    $this->error('修改失败');
                }
            } else {
                $this->error($attrval->getError());
            }
            }else{
                $id = I('get.id');
                $gid = I('get.gid');
                $kid = I('get.kid');
                $goods = M('goods')->where(array('id'=>$gid))->select();
                $attr = M('attr')->where(array('id'=>$kid))->select();
                $res = M('attrval')->where(array('id'=>$id))->select();
                $this->meta_title = '修改属性值';
                $this->assign('res',$res[0]);           
                $this->assign('goods',$goods[0]);           
                $this->assign('attr',$attr[0]);           
                $this->display();
            }
    }

}